﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MODEL;

namespace BL
{
    public class ProductManagment
    {
        public bool AddNewProduct(Product p)
        {
            dbKassaDataContext db = new dbKassaDataContext();
            var result = from pro in db.Products where pro.name == p.name && pro.price == p.price select pro;
            List<Product> addNewList = result.ToList();

            if (addNewList.Count == 0)
            {
                db.Products.InsertOnSubmit(p);
                db.SubmitChanges();
                return true;
            }
            else return false;
        }

        public bool EditProduct(Product p)
        {
            dbKassaDataContext db = new dbKassaDataContext();
            var result = from pro in db.Products where pro.name == p.name select pro;
            List<Product> editProductList = result.ToList();

            if (editProductList.Count == 1)
            {
                editProductList[0].name = p.name;
                editProductList[0].price = p.price;
                editProductList[0].VATID = p.VATID;
                db.SubmitChanges();
                return true;
            }
            else return false;
        }

        public bool RemoveProduct(Product p)
        {
            dbKassaDataContext db = new dbKassaDataContext();
            var result = from pro in db.Products where pro.name == p.name select pro;
            List<Product> removeProductList = result.ToList();
            if (removeProductList.Count != 0)
            {
                db.Products.DeleteOnSubmit(removeProductList[0]);
                return true;
            }
            else return false;
        }

        public List<Product> SelectAllProducts()
        {
            dbKassaDataContext db = new dbKassaDataContext();
            var result = from p in db.Products select p;
            List<Product> allProductList = result.ToList();
            return allProductList;
        }

    }
}
